﻿namespace TriangleGames.TrainRes
{
    using System;
    using UnityEngine;

    public abstract class FsmStateAction : IFsmStateAction
    {
        private bool enabled = true;
        private bool finished;
        [NonSerialized]
        private TriangleGames.TrainRes.Fsm fsm;
        [NonSerialized]
        private FsmState fsmState;
        private bool isOpen = true;
        private string name;
        private GameObject owner;

        protected FsmStateAction()
        {
        }

        public void ChangeState(FsmEvent fsmEvent)
        {
            this.fsm.ChangeState(fsmEvent);
        }

        public void ChangeState(string eventName)
        {
            this.fsm.ChangeState(FsmEvent.GetFsmEvent(eventName));
        }

        public virtual void DoCollisionEnter(Collision collisionInfo)
        {
        }

        public virtual void DoCollisionExit(Collision collisionInfo)
        {
        }

        public virtual void DoCollisionStay(Collision collisionInfo)
        {
        }

        public virtual void DoControllerColliderHit(ControllerColliderHit collider)
        {
        }

        public virtual void DoTriggerEnter(Collider other)
        {
        }

        public virtual void DoTriggerExit(Collider other)
        {
        }

        public virtual void DoTriggerStay(Collider other)
        {
        }

        public virtual string ErrorCheck()
        {
            return string.Empty;
        }

        public void Finish()
        {
            this.finished = true;
        }

        public virtual void Init(FsmState state)
        {
            this.fsmState = state;
            this.fsm = state.Fsm;
            this.owner = this.fsm.GameObject;
        }

        public void Log(string text)
        {
            if (FsmLog.LoggingEnabled)
            {
                this.fsm.MyLog.LogAction(FsmLogType.Info, text);
            }
        }

        public void LogError(string text)
        {
            if (!FsmLog.LoggingEnabled)
            {
                this.fsm.MyLog.LogAction(FsmLogType.Error, text);
            }
        }

        public void LogWarning(string text)
        {
            if (!FsmLog.LoggingEnabled)
            {
                this.fsm.MyLog.LogAction(FsmLogType.Warning, text);
            }
        }

        public virtual void OnDrawGizmos()
        {
        }

        public virtual void OnDrawGizmosSelected()
        {
        }

        public virtual void OnEnter()
        {
        }

        public virtual void OnExit()
        {
        }

        public virtual void OnFixedUpdate()
        {
        }

        public virtual void OnGUI()
        {
        }

        public virtual void OnLateUpdate()
        {
        }

        public virtual void OnUpdate()
        {
        }

        public virtual void Reset()
        {
        }

        public bool Active
        {
            get
            {
                return (this.enabled && !this.finished);
            }
        }

        public bool Enabled
        {
            get
            {
                return this.enabled;
            }
            set
            {
                this.enabled = value;
            }
        }

        public bool Finished
        {
            get
            {
                return this.finished;
            }
            set
            {
                this.finished = value;
            }
        }

        public TriangleGames.TrainRes.Fsm Fsm
        {
            get
            {
                return this.fsm;
            }
            set
            {
                this.fsm = value;
            }
        }

        public bool IsOpen
        {
            get
            {
                return this.isOpen;
            }
            set
            {
                this.isOpen = value;
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        public GameObject Owner
        {
            get
            {
                return this.owner;
            }
            set
            {
                this.owner = value;
            }
        }

        public FsmState State
        {
            get
            {
                return this.fsmState;
            }
            set
            {
                this.fsmState = value;
            }
        }
    }
}

